/*
 * Copyright (c) 2024 OpenValley(ShenZhen) Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

#include <los_task.h>
#include <los_arch_interrupt.h>

#include <ohos_init.h>
#include <ohos_types.h>

#include <hiview_log.h>
#include <uart_if.h>
#include <gpio_if.h>
#include <board_config.h>
#include "uart_rw_obu.h"
#include "app_att_obu.h"
#include "app.h"
#define UART_TASK_PRIORITY LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO-5
DevHandle *uart_handle;

void obu_uart_init(uint32_t port, uint32_t baud)
{
    /* See device/soc/telink/b91/hcs/uart/uart_config.hcs
       for hdf uart configuration */
    uart_handle = UartOpen(port);

    if (uart_handle == NULL) {
        printf("UartOpen %u: failed!\n", port);
    }
    UartSetBaud(uart_handle, baud);
    UartGetBaud(uart_handle, &baud);
    printf("%s: Set baudrate %u\r\n", __func__, baud);
}

uint32_t obu_uart_read(char *rxbuf, uint32_t len)
{
    uint32_t ret = UartRead(uart_handle, rxbuf, len); 
    if (ret <= 0) { // =0 no data; <0 error
        printf("UartRead %u: failed!\r\n", uart_handle);
    }
    return ret;
}

uint32_t obu_uart_write(char *txbuf, uint32_t len)
{
    uint32_t ret = UartWrite(uart_handle, txbuf, len);
    if (ret != 0) {
        printf("UartWrite %u: failed!\r\n", uart_handle);
    }
    return ret;
}

STATIC VOID uart_rw_task(VOID)
{
    obu_uart_init(UART1,BAUD_921600); 
    obu_uart_write(OBU_TXBUF,strlen((char *)OBU_TXBUF));
    short count = 0;
    char str_notify[20];
    int ret = 0;
   // 开启新的线程执行notify，隔大概32s会遇到 [ERR][IdleCore000]ASSERT ERROR! 
   // ../../../kernel/liteos_m/kernel/src/los_sched.c, 78, OsTimeSliceUpdate
    while(1)
    {
        LOS_TaskDelay(2000);
        if(IS_CONNECT_FLAG)
        {
            sprintf(str_notify,"%s%d","uart_rw_task notify = ",count);
            notify(str_notify, strlen(str_notify));
            printf("uart_rw_task notify\r\n");
            count +=1;
        }
        obu_uart_write(OBU_TXBUF,strlen((char *)OBU_TXBUF));
        printf("uart_rw_task\r\n");
    }
   
}

void UartAppMain(void)
{
    /*waiting AcceKit initialization */
    LOS_TaskDelay(8000);
    UINT32 ret;
    UINT32 taskId = 1;

    TSK_INIT_PARAM_S taskParam = {0};
    taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)uart_rw_task;
    taskParam.uwArg = 0;
    taskParam.uwStackSize = LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE;
    taskParam.pcName = "uart_rw_task";
    taskParam.usTaskPrio = UART_TASK_PRIORITY;

    ret = LOS_TaskCreate(&taskId, &taskParam);
    if (ret != LOS_OK) {
        HILOG_ERROR(HILOG_MODULE_APP, "ret of LOS_TaskCreate(uart_task) = %#x", ret);
    }
}

SYS_RUN(UartAppMain);
